package com.xidian.zhiancloud.controller.workTicket;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dao.SysJobMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.dto.workTicket.ElectricStaffDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.SysJob;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.model.workTicket.ElectricFlow;
import com.xidian.zhiancloud.service.workTicket.ElectricFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("ElectricFlow")
public class ElectricFlowController extends BaseController {
    @Autowired
    private ElectricFlowService electricFlowService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysJobMapper sysJobMapper;

    // 人员设置接口：前端传进来动火作业的subId，<选择标准，具体ids>，即Map<Integer, String>
    @PostMapping("/setElectricStaff")
    public ResultDTO<Boolean> setElectricStaff(Integer subId, Integer selectStandard, String selectPerson) {
        Long companyId = getSessionUserCompanyId();
        // 获取electricFlowid
        Long electricFlowId = electricFlowService.getFlowBySubId(companyId, subId);
        ElectricFlow electricFlow = electricFlowService.getById(electricFlowId);
        // 如果当前数据的字段已经非空,且与新填入的不同，则判错
        if ((electricFlow.getSelectStandard()!= null) && (electricFlow.getSelectStandard() != selectStandard)) {
            return ResultDTO.errorOf(GeneralResponseEnums.SUBMIT_FAILED);
        } else {
            electricFlow.setSelectStandard(selectStandard);
        }
        // 如果当前selectPerson非空，则加","
        if (electricFlow.getSelectPerson() != null && electricFlow.getSelectPerson().length() != 0) {
            selectPerson = electricFlow.getSelectPerson() + ","+ selectPerson;
        }
        electricFlow.setSelectPerson(selectPerson);
        Boolean res = electricFlowService.updateById(electricFlow);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }


    // 查看人员设置接口：返回人员和职位的中文名称
    @GetMapping("getElectricStaff")
    public ResultDTO<ElectricStaffDTO> getElectricStaff() {
        Long companyId = getSessionUserCompanyId();
        String PersonList = "";
        // 返回值类型
        ElectricStaffDTO res = new ElectricStaffDTO();
        // 名字
        Map<Integer, List<String>> electricFlowMap = new LinkedHashMap<>();
        // id
        Map<Integer, List<Long>> electricFlowIds = new LinkedHashMap<>();
        // 选择标准
        List<Integer> standards = new LinkedList<>();
        LambdaQueryWrapper<ElectricFlow> query = Wrappers.<ElectricFlow>lambdaQuery();
        query.eq(ElectricFlow::getCompanyId, companyId);
        // 获取一个公司，所有的15条fireFLow记录
        List<ElectricFlow> electricFlows = electricFlowService.list(query);
        // 遍历，填充map
        for (int i = 1; i < electricFlows.size(); i++) {
            // 查找人员列表
            String ids_s = electricFlows.get(i).getSelectPerson();
            // 判断是通过人员 / 职位 进行id ——> string
            if (electricFlows.get(i).getSelectStandard() == 0) {        // 人员
                long[] ids_l = Arrays.stream(ids_s.split(",")).mapToLong(Long::parseLong).toArray();
                List<String> names = Arrays.stream(ids_l).mapToObj(tmp_id -> {
                    SysUser workers = sysUserMapper.selectById(tmp_id);
                    return workers == null ? "该用户已被删除" : workers.getName();
                }).collect(Collectors.toList());
                electricFlowMap.put(i+1, names);

                // 添加人员id
                List<Long> ids = new LinkedList<>();
                if(ids_l.length != 0){
                    for (long l : ids_l) {
                        ids.add(l);
                    }
                }
                electricFlowIds.put(i+1, ids);
            } else {                                                // 职位
                long[] ids_l = Arrays.stream(ids_s.split(",")).mapToLong(Long::parseLong).toArray();
                List<String> names = Arrays.stream(ids_l).mapToObj(tmp_id -> {
                    SysJob sysJob = sysJobMapper.selectById(tmp_id);
                    return sysJob == null ? "该职位已被删除" : sysJob.getPostName();
                }).collect(Collectors.toList());
                // 添加职位id
                List<Long> ids = new LinkedList<>();
                if(ids_l.length != 0){
                    for (long l : ids_l) {
                        ids.add(l);
                    }
                }
                electricFlowMap.put(i+1, names);
            }
            standards.add(electricFlows.get(i).getSelectStandard());
        }
        res.setStaffNames(electricFlowMap);
        res.setStaffIds(electricFlowIds);
        res.setSelectStandards(standards);

        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }


}
